﻿namespace AgileZen.Models
{
    using System;
    using System.Runtime.Serialization;

    [DataContract(Name = "invite")]
    public class Invite : AgileZenModel
    {
        private string _email;
        private string _token;
        private Role _role;

        //catch date first in a string, because the JSON serializer expects a Date(*) notation
        [DataMember(Name = "createTime", Order = 1)]
        private string CreatedOnUTCString { get; set; }

        [DataMember(Name = "id", Order = 0)]
        public int Id { get; set; }

        public DateTime? CreatedOn
        {
            get
            {
                bool parsed = false;
                DateTime datetime = DateTime.MinValue;
                if (!String.IsNullOrEmpty(CreatedOnUTCString))
                {
                    parsed = DateTime.TryParse(CreatedOnUTCString, out datetime);
                }

                return (parsed) ? datetime : new Nullable<DateTime>();
            }
        }

        [DataMember(Name = "email", Order = 2, EmitDefaultValue = false)]
        public string Email
        {
            get { return _email; }
            set 
            { 
                _email = value; 
                NotifyPropertyChanged("Email"); 
            }
        }

        public bool ShouldSerializeEmail()
        {
            return PropertiesChanged.Contains("Email");
        }

        [DataMember(Name = "token", Order = 3, EmitDefaultValue = false)]
        public string Token
        {
            get { return _token; }
            set 
            { 
                _token = value; 
                NotifyPropertyChanged("Token"); 
            }
        }

        public bool ShouldSerializeToken()
        {
            return PropertiesChanged.Contains("Token");
        }

        [DataMember(Name = "role", Order = 4, EmitDefaultValue = false)]
        public Role Role
        {
            get { return _role; }
            set 
            { 
                _role = value; 
                NotifyPropertyChanged("Role"); 
            }
        }

        public bool ShouldSerializeRole()
        {
            return PropertiesChanged.Contains("Role");
        }
    }
}
